package cn.sibu.plugin.authorize.util;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.sibu.plugin.authorize.R;

/**
 * Created by 刘红亮 on 2015/5/26 11:47
 */
public class DialogUtil {


    /** 加载数据对话框 */
    private static Dialog mLoadingDialog;
    /**
     * 显示加载对话框
     * @param context 上下文
     * @param msg 对话框显示内容
     * @param cancelable 对话框是否可以取消
     */
    public static void showDialogForLoading(Context context, String msg, boolean cancelable) {
        dismissDialogForLoading();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null);
        TextView loadingText = (TextView)view.findViewById(R.id.id_tv_loading_dialog_text);
        loadingText.setText(msg);
        mLoadingDialog = new Dialog(context, R.style.loading_dialog_style);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    /**
     * 关闭加载对话框
     */
    public static void dismissDialogForLoading() {
        if(mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
    }
    private static Dialog returnSibuDialog;

    /**
     * 重新登录提示对话框
     */
    public static void ShowReLogin(Context context){
        // 自定义对话框布局
        View view2 = View.inflate(context, R.layout.dialog_repetlogin,null);
        // new一个对话框并传入风格，里面定义了对话框显示属性
        final Dialog dialogShow2 = new Dialog(context,R.style.dialog_show_style);
        dialogShow2.setContentView(view2);
        dialogShow2.setCancelable(false);
        dialogShow2.show();
        Window window = dialogShow2.getWindow();
        window.setGravity(Gravity.CENTER);// 底部0
        window.setWindowAnimations(R.style.my_style);// 效果
        Button bt_repetlogin = (Button) view2.findViewById(R.id.bt_repetlogin);

        //确定
        bt_repetlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogShow2.dismiss();//关闭对话框窗口
            }
        });
    }

    /**
     * 返回手机思埠对话框
     * @param context
     * @param imgres
     * @param hintStr
     * @return
     */
    public static TextView ShowreturnSibuDialog(Context context,int imgres,String hintStr){

        View view =	View.inflate(context, R.layout.dialog_shouquan, null);

        returnSibuDialog = new Dialog(context,R.style.dialog_show_style);
        returnSibuDialog.setContentView(view);
        returnSibuDialog.setCancelable(false);//返回键处理
        returnSibuDialog.show();
        Window window = returnSibuDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.my_style);// 效果
        ImageView hint_icon = (ImageView) view.findViewById(R.id.hint_icon);
        TextView confirm = (TextView) view.findViewById(R.id.confirm);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        TextView hint_content = (TextView) view.findViewById(R.id.hint_content);
        //设置图片
        hint_icon.setBackgroundResource(imgres);

        //设置提示语
        hint_content.setText(hintStr);

        //取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnSibuDialog.dismiss();
            }
        });
        return confirm;
    }

    public static void dismissReturnSibuDialog(){
        returnSibuDialog.dismiss();
    }

}
